package com.twang.stock.task.old;

import com.jayway.jsonpath.JsonPath;
import com.twang.stock.entity.CompanyCodeInfo;
import com.twang.stock.mapper.CompanyCodeInfoMapper;
import com.twang.stock.utils.http.HttpStockUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 分析个股趋势 每日16点00分更新
 * request：http://zs.jrj.com.cn/smartstock/service/trend/queryTrendDetail/sz/000069.jspa
 * response：{"msg":"success","data":{"market":"sz","record":[20201225,20210104,20210112,20210120,20210201,20210225,20210226,20210304,20210308,20210319,20210325,20210330],"stkcode":"000069","stkname":"华侨城Ａ","stktype":"stock","strength":5,"subTypeDesc":"03-30，股价形成强势上升趋势","subTypeId":200012,"subTypeName":"强势上升股","trendTypeId":2,"trendTypeName":"强势上升","updateTime":20210330,"weight":10059.88},"retCode":0}
 */
@Slf4j
@Service
public class AnalyseStockTendTask extends  BaseTask{

    @Resource
    private CompanyCodeInfoMapper companyCodeInfoMapper;

    public void execut(){
        if (!isWork()) {
            return;
        }
       List<CompanyCodeInfo> stockList= companyCodeInfoMapper.selectAll();
       stockList.forEach(item-> {
           String code = item.getCompanyCode().substring(2, 8);
           String market = item.getCompanyCode().substring(0, 2);
           String url = "http://zs.jrj.com.cn/smartstock/service/trend/queryTrendDetail/" + market + "/" + code + ".jspa";
           HttpStockUtils httpUtils = new HttpStockUtils();
           String result = "";
           try {
               result = httpUtils.getForm(url);
               update(result,item);
           } catch (Exception e) {
               e.printStackTrace();
           }
       });
    }

    private  void update(String result,CompanyCodeInfo item){
        try {
            String subTypeDesc = JsonPath.parse(result).read("$.data.subTypeDesc");
            item.setSubTypeDesc(subTypeDesc);
        } catch (Exception e) { }
        try {
            String subTypeName = JsonPath.parse(result).read("$.data.subTypeName");
        } catch (Exception e) {  }

        try {
            String trendTypeName = JsonPath.parse(result).read("$.data.trendTypeName");
            item.setTrendTypeName(trendTypeName);
        } catch (Exception e) { }
        try {
            String updateTime = JsonPath.parse(result).read("$.data.updateTime") + "";
            item.setUpdateTime(updateTime);
        } catch (Exception e) { }
        try {
            String weight = JsonPath.parse(result).read("$.data.weight")+"";
            item.setWeight(StringUtils.isEmpty(weight)? BigDecimal.ZERO:new BigDecimal(weight));
        } catch (Exception e) { }
        item.setModifyTime(new Date());
        companyCodeInfoMapper.updateByPrimaryKeySelective(item);
    }

    public static void main(String[] args) {
        String code ="000001";
        String market = "sh";
        String url = "http://zs.jrj.com.cn/smartstock/service/trend/queryTrendDetail/" + market + "/" + code + ".jspa";
        HttpStockUtils httpUtils = new HttpStockUtils();
        String result = "";
        try {
            result = httpUtils.getForm(url);
           // update(result,item);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
